package com.woorigil.social.controller.admin.flower;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.flower.Flower;
import com.woorigil.social.domain.flower.FlowerCondition;
import com.woorigil.social.domain.flower.FlowerDao;
import com.woorigil.social.domain.flower.FlowerTagRef;
import com.woorigil.social.domain.flower.FlowerTagRefDao;
import com.woorigil.social.domain.flower.Tag;
import com.woorigil.social.domain.flower.TagDao;

@Controller
@RequestMapping("/admin/koreaflower")
public class FlowerController extends BaseController {
	@Autowired FlowerDao flowerDao;
	@Autowired FlowerTagRefDao flowerTagRefDao;
	@Autowired TagDao tagDao;
	
	@RequestMapping("/flower/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		Flower flower = flowerDao.select(id);
		List<Tag> tags = tagDao.search();
		List<FlowerTagRef> flowerTagRefs = flowerTagRefDao.selectListByFlowerId(id);
		
		for (int i=flowerTagRefs.size(); i<4; i++) {
			flowerTagRefs.add(new FlowerTagRef());
		}
		
		ModelAndView mav = new ModelAndView(getForwordPage("admin/koreaflower/flower/form"));
		mav.addObject("flower", flower);
		mav.addObject("tags", tags);
		mav.addObject("flowerTagRefs", flowerTagRefs);
		return mav;
	}

	@RequestMapping("/flower/insert")
	public ModelAndView insert(Flower flower, HttpServletRequest request) throws Exception {
		int id = flowerDao.insert(flower);
		
		ModelAndView mav = new ModelAndView(getRedirectUrl("/flower/view"));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/flower/update")
	public ModelAndView update(Flower flower, int[] tagIds) throws Exception {
		flowerDao.update(flower);
		if (tagIds != null) {
			flowerTagRefDao.deleteByFlowerId(flower.getId());
			for (int tagId : tagIds) {
				flowerTagRefDao.insert(new FlowerTagRef(flower.getId(), tagId));
			}
		}
		
		ModelAndView mav = new ModelAndView(getRedirectUrl("/admin/koreaflower/flower/list"));
		mav.addObject("id", flower.getId());
		return mav;
	}
	
	@RequestMapping("/flower/delete")
	public ModelAndView delete(int id) {
		flowerDao.delete(id);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/flower/list"));
		return mav;
	}
	
	@RequestMapping("/flower/view")
	public ModelAndView view(int id) {
		Flower flower = flowerDao.select(id);
		if (flower == null) {
			throw new IllegalArgumentException("message");
		}
		ModelAndView mav = new ModelAndView("flower/view");
		mav.addObject("flower", flower);
		return mav;
	}	
	
	@RequestMapping("/flower/list")
	public ModelAndView list(FlowerCondition condition) {
		List<Flower> flowerList = flowerDao.search(condition);
		
		ModelAndView mav = new ModelAndView(getForwordPage("admin/koreaflower/flower/list"));
		mav.addObject("condition", condition);
		mav.addObject("flowerList", flowerList);
		return mav;
	}
}